<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\unit\controller;

use think\Db;
use think\Cache;

class Car extends Base
{
	protected $id = 'car_id';
	protected $table = 'car';
	protected $join = ['company c','c.company_id = m.company_id','left'];

    protected function parseData($data = []) {

        //公司ID关联
        $data['company_id'] = $this->company['company_id'];
        $data = array_map('Arr2Set',$data);

        return $data;
    }

    protected function options($item = [])
    {
        //车辆性质
        $prop = config('base.car_prop');
        $props = $item['prop'] ? explode(',',$item['prop']) : [];
        foreach($prop AS $i => $p) {
            $prop[$i]['open'] = true;
            if($props && in_array($p['id'],$props)) $prop[$i]['checked'] = true;
        }
        $this->assign('props',json_encode($prop));

        //品牌
        $car_brands = Cache::remember('car_brands',function(){
            $car_brands = Db::name('car_brand')->where('level',1)->column('CONCAT(`type`,`name`) AS name','car_brand_id');
            return $car_brands;
        });
        //车型
        $car_models = [];
        if($item['car_brand_id']) {
            $car_models = Db::name('car_brand')->where('parent_id',$item['car_brand_id'])->fetchSql(0)->column('name','car_brand_id');
        } else {
            $car_models = Cache::remember('car_models',function(){
                $car_models = Db::name('car_brand')->where('level',2)->column('name','car_brand_id');
                return $car_models;
            });
        }
        //系列
        $car_seriess = [];
        if($item['car_model_id']) {
            $car_series = Db::name('car_brand')->where('parent_id',$item['car_model_id'])->fetchSql(0)->column('name','car_brand_id');
        } else {
            $car_series = Cache::remember('car_series',function(){
                $car_series = Db::name('car_brand')->where('level',3)->column('name','car_brand_id');
                return $car_series;
            });
        }

        //系列
        $car_seriess = [];
        if($item['car_model_id']) {
            $car_series = Db::name('car_brand')->where('parent_id',$item['car_model_id'])->fetchSql(0)->column('name','car_brand_id');
        } else {
            $car_series = Cache::remember('car_series',function(){
                $car_series = Db::name('car_brand')->where('level',3)->column('name','car_brand_id');
                return $car_series;
            });
        }


        //区域
        $area_ids = Db::name('area')->select();
//		$area_ids = Arr2Sibling($area_ids,0,0,'area_id','area_pid');
        $this->assign('area_ids',TplOptions($area_ids,$item['area_pid'],'area_name','area_id'));

        $this->assign('car_brands',$car_brands);
        $this->assign('car_brand_ids',TplOptions($car_brands,$item['car_brand_id']));
        $this->assign('car_models',$car_models);
        $this->assign('car_model_ids',TplOptions($car_models,$item['car_model_id']));
        $this->assign('ar_series',$car_series);
        $this->assign('car_series_ids',TplOptions($car_series,$item['car_series_id']));
        $this->assign('energy_types',TplOptions(config('base.energy_types'),$item['energy_type']));
        $this->assign('sources',TplOptions(config('base.sources'),$item['source']));
        $this->assign('structures',TplOptions(config('base.structures'),$item['structure']));
        $this->assign('gearbox_types',TplOptions(config('base.gearbox_types'),$item['gearbox_type']));

        return parent::options($item); // TODO: Change the autogenerated stub
    }

}
